(module store (lib "eopl.ss" "eopl")
  
  (require "drscheme-init.scm")
  
  (provide initialize-store! reference? newref deref setref! 
    instrument-newref get-store-as-list)
  
  (define instrument-newref (make-parameter #f))
  
  ;;;;;;;;;;;;;;;; references and the store ;;;;;;;;;;;;;;;;
  
  ;;; world's dumbest model of the store:  the store is a list and a
  ;;; reference is number which denotes a position in the list.
  
  (define empty-store
    (lambda () '()))
  
  (define the-store (empty-store))
  
  (define get-store-as-list
    (lambda () the-store))
  
  (define initialize-store!
    (lambda ()
      (set! the-store (empty-store))))
  
  (define reference? integer?)
  
  (define newref
    (lambda (val)
      (let ((next-ref (length the-store)))
        (set! the-store
              (append the-store (list val)))
        (if (instrument-newref)
            (eopl:printf 
             "newref: allocating location ~s~%"
             next-ref))                     
        next-ref)))                     ; YUCK!
  
  (define deref 
    (lambda (ref) (list-ref the-store ref)))
  
  (define setref!                       ; YUCK!
    (lambda (ref0 val)
      (set! the-store
            (let recur ((store the-store) (ref ref0))
              (cond
                ((null? store)
                 (eopl:error 'setref
                             "illegal reference ~s in store ~s"
                             ref0 the-store))
                ((zero? ref)
                 (cons val (cdr store)))
                (else
                 (cons
                  (car store)
                  (recur (cdr store) (- ref 1)))))))))

  )